import { stringify } from 'qs';
import request from '@/utils/request';
import { withProcessedData } from '@/utils/marketingMap/utils';

const basePath = '/msmarketingmap/aftersales/inflow';

// 1.专营店流入情况及统计概况
export const fetchInflowStore = async (query: QueryProps) =>
  withProcessedData(query, async query => {
    const result = await request(`${basePath}/store?${stringify(query)}`);

    if (result.statusCode !== '0') return {};

    return result.data;
  });
// 2.流入趋势图
export const fetchInflowStoreTrend = async (query: QueryProps) =>
  withProcessedData(query, async query => {
    const result = await request(`${basePath}/store/trend?${stringify(query)}`);

    if (result.statusCode !== '0') return [];

    return result.data;
  });

// 3.流入用户画像-车系分布
export const fetchPersonaSeries = async (query: QueryProps) =>
  withProcessedData(query, async query => {
    const result = await request(`${basePath}/persona/series?${stringify(query)}`);

    if (result.statusCode !== '0') return [];

    return result.data;
  });

// 4.流入用户画像-售后项目分布
export const fetchPersonaProject = async (query: QueryProps) =>
  withProcessedData(query, async query => {
    const result = await request(`${basePath}/persona/project?${stringify(query)}`);

    if (result.statusCode !== '0') return [];

    return result.data;
  });

// 5.流入用户画像-用户类型分布
export const fetchPersonaUserType = async (query: QueryProps) =>
  withProcessedData(query, async query => {
    const result = await request(`${basePath}/persona/user/type?${stringify(query)}`);

    if (result.statusCode !== '0') return [];

    return result.data;
  });

export const fetchPersonaPortraitInfo = async (query: QueryProps) =>
  withProcessedData(query, async query => {
    const result = await request(
      `/msmarketingmap/aftersales/inflow/persona/portrait/info?${stringify({
        ...query,
        inout: 1,
        tags: 7,
      })}`,
    );

    if (result.statusCode !== '0') return [];

    return result.data;
  });

export const fetchPersona = async (query: QueryProps) => {
  return Promise.all([
    fetchPersonaSeries(query),
    fetchPersonaProject(query),
    fetchPersonaUserType(query),
    fetchPersonaPortraitInfo(query)
  ]);
};
